package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IDispatchEngineeringOrderListView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.DispatchEngineeringOrderListPresenter;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringOrderListAdapter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 工程》工单管理》 分派工单
 * Created by tony on 2018/1/26.
 */

public class DispatchEngineeringOrderListActivity extends Activity implements View.OnClickListener, IDispatchEngineeringOrderListView {
    private View rl_back;
    private TextView tv_title;
    private View rl_progressbar;

    private ListView lv;
    private EngineeringOrderListAdapter adapter;
    private List<OrderParentBean> engineeringOrderList = new ArrayList<>();
    private DispatchEngineeringOrderListPresenter dispatchEngineeringOrderListPresenter;
    private final String TAG = DispatchEngineeringOrderListActivity.class.getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineering_order_dispatch_list);

        dispatchEngineeringOrderListPresenter = new DispatchEngineeringOrderListPresenter(this);
        init();
        getOrderList();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);

        lv = findViewById(R.id.lv);

        tv_title.setText(getResources().getText(R.string.assigned_order));
        rl_back.setOnClickListener(this);

        adapter = new EngineeringOrderListAdapter(this, TAG);
        adapter.setData(engineeringOrderList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent mIntent;
                OrderParentBean order = engineeringOrderList.get(position);
                // 工程安装
                if (order.getCategory() == CommonEnum.EngineeringORderCategory.INSTALL) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, DispatchEngineeringInstallationOrderActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                    mIntent.putExtra(ConstantUtil.KEY_2, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 工程施工
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, DispatchEngineeringConstructOrderActivity.class);
                    startActivity(mIntent);
                }
                // 批量安装
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, DispatchEngineeringBatchInstallationOrderActivity.class);
                    startActivity(mIntent);
                }
                // 维修单
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, ReceiveMaintenanceOrderActivity.class);
                    startActivity(mIntent);
                }
                // 广告上刊
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, DispatchEngineeringInstallationOrderActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                    mIntent.putExtra(ConstantUtil.KEY_2, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 巡查
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.PATROL) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, DispatchPatrolOrderActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                    mIntent.putExtra(ConstantUtil.KEY_2, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 现场勘查
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.SURVEY) {
                    mIntent = new Intent(DispatchEngineeringOrderListActivity.this, DispatchEngineeringInstallationOrderActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                    mIntent.putExtra(ConstantUtil.KEY_2, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }

            }
        });
    }


    private void getOrderList() {
        dispatchEngineeringOrderListPresenter.getEngineeringDispatchOrderList();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    getOrderList();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;

        }
    }

    @Override
    public void getDataSuccessed(List<OrderParentBean> engineeringOrderBeanLs) {
        if (null != engineeringOrderBeanLs && engineeringOrderBeanLs.size() > 0) {
            engineeringOrderList.clear();
            engineeringOrderList.addAll(engineeringOrderBeanLs);
            adapter.setData(engineeringOrderList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }
}
